#include <iostream>
#include "Generator.h"

using namespace std;

GeneratorImp::GeneratorImp(EventProcessor& _master, DistributedCounter& _dCounter)
  :dCounter(_dCounter), counter(1)
#ifdef DEBUG_EVPROC
, EventProcessorImp(true,"Generator") // comment to remove debug
#endif
{
  Master.CopyFrom(_master);
  RegisterMessageProcessor(NextNumberMessage::type, &NumberAdded, 1 /*priority*/);
  GenerateAndSendNumber();
}

void GeneratorImp::GenerateAndSendNumber(void){

  EventProcessor myInterfClone;
  myInterfClone.CopyFrom(myInterface);
  FillMessage_Factory(Master, counter, myInterfClone);

}

MESSAGE_HANDLER_DEFINITION_BEGIN(GeneratorImp, NumberAdded, NextNumberMessage)
  evProc.counter++;
  if (evProc.counter <= REPETITIONS){

    evProc.GenerateAndSendNumber();
  } else {
	  if (evProc.dCounter.Decrement(1) == 0){
		  cout<<"I am the last Generator - GenDone"<<endl;
		  EventProcessor myInterfClone;
		  myInterfClone.CopyFrom(evProc.myInterface);
		  GenDoneMessage_Factory(msg.sender, 0, myInterfClone);
	  }

	  evProc.Seppuku();
  }
MESSAGE_HANDLER_DEFINITION_END

